
package chapter3;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;

public class Ex7 extends JApplet {
  public static void main(String s[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JApplet applet = new Ex7();
    applet.init();
    frame.getContentPane().add(applet);
    frame.pack();
    frame.setVisible(true);
  }
  
  public void init() {
    JPanel panel = new Ex7Panel();
    getContentPane().add(panel);
  }
}

class Ex7Panel extends JPanel {
  public Ex7Panel() {
    setPreferredSize(new Dimension(400, 400));
    setBackground(Color.white);
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    int w = getWidth();
    int h = getHeight();
    float r = Math.min(w, h)/2;
    AffineTransform tx = new AffineTransform();
    tx.rotate(Math.PI/4);
    tx.scale(r, r);
    Rectangle2D rect = new Rectangle2D.Double(-0.5, -0.5, 1, 1);
    Shape shape = tx.createTransformedShape(rect);
    g2.translate(w/2, h/2);
    g2.draw(shape);
  }
}